package org.csu.mypetstoreapi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpSession;
import org.csu.mypetstoreapi.common.CommonResponse;
import org.csu.mypetstoreapi.config.CorsConfig;
import org.csu.mypetstoreapi.entity.Account;
import org.csu.mypetstoreapi.entity.Category;
import org.csu.mypetstoreapi.entity.Item;
import org.csu.mypetstoreapi.entity.Product;
import org.csu.mypetstoreapi.persistence.CategoryMapper;
import org.csu.mypetstoreapi.persistence.ItemMapper;
import org.csu.mypetstoreapi.persistence.ProductMapper;
import org.csu.mypetstoreapi.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//@MapperScan("org.csu.jpetstore_user.persistence")
@RequestMapping("/catalog")
@CrossOrigin
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    //主页显示信息
    @GetMapping("/mainForm")
    @ResponseBody
    public CommonResponse<List<Product>> home(HttpSession session, Model model){
        return catalogService.findAllProducts();
    }


    //首页点击详情按钮进入productForm
    @GetMapping("/products/{id}")
    @ResponseBody
    public CommonResponse<Product> productForm(@PathVariable("id") String id, HttpSession session){
       return catalogService.findProductByProductId(id);
    }

    //通过productId获取itemList
    @GetMapping("/itemList/{id}")
    @ResponseBody
    public CommonResponse<List<Item>> itemList(@PathVariable("id") String id, HttpSession session){
        return catalogService.findItemByProductId(id);
    }

    //通过itemId获取该item的信息
    @GetMapping("/item/{id}")
    @ResponseBody
    public CommonResponse<Item> itemById(@PathVariable("id") String id){
        return catalogService.findItemById(id);
    }

    //用户退出登录
    @GetMapping("/signOut")
    @ResponseBody
    public CommonResponse<Account> signOut(HttpSession session){
        session.removeAttribute("loginUser");
        session.removeAttribute("account");
        session.removeAttribute("login_account");
        return CommonResponse.createForSuccessMessage("退出成功");
    }

    //搜索进入searchProducts
    @GetMapping("/search/{keyword}")
    @ResponseBody
    public CommonResponse<List<Product>> search(@PathVariable("keyword") String keyword) {
       return catalogService.searchProductList(keyword);
    }

    //获取所有categories
    @GetMapping("/allCategory")
    @ResponseBody
    public CommonResponse<List<Category>> getAllCategories(){
        return catalogService.findAllCategorys();
    }


}
